package com.woniuxy.util;

import com.netflix.client.config.IClientConfig;
import com.netflix.loadbalancer.AbstractLoadBalancerRule;
import com.netflix.loadbalancer.ILoadBalancer;
import com.netflix.loadbalancer.Server;

import java.util.List;

public class MyRule extends AbstractLoadBalancerRule {
    @Override
    public void initWithNiwsConfig(IClientConfig iClientConfig) {
        //不用管
    }
    int index=0;
    int currentTime=1;
    int maxTime=3;
    @Override
    public Server choose(Object o) {
        //获取负载均衡器，其中封装了所有服务
        ILoadBalancer loadBalancer = this.getLoadBalancer();
        //获取所有服务
        List<Server> allServers = loadBalancer.getAllServers();
        Server server=null;
        if (currentTime<=maxTime){
            server=allServers.get(index);
            currentTime++;
        }else {
            index++;
            currentTime=2;
            if (index>allServers.size()-1){
                index=0;
            }
            server = allServers.get(index);
        }
        return server;
    }
}
